package com.cos.hospital.ui.patient;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.cos.hospital.R;
import com.cos.hospital.code.Code;
import com.cos.hospital.code.helper.MyOnFreshListener;
import com.cos.hospital.init.BaseActivity;
import com.cos.hospital.model.UHttp;
import com.cos.hospital.model.bean.ErrorBean;
import com.cos.hospital.model.bean.HistoryRecordsBean;
import com.cos.hospital.model.bean.HistoryRecordsItemsBean;
import com.cos.hospital.model.bean.ResultBean;
import com.cos.hospital.model.connector.HttpMsgListener;
import com.cos.hospital.tool.MyUtil;
import com.cos.hospital.tool.USnackbar;
import com.cos.hospital.tool.UTxt;
import com.cos.hospital.ui.patient.adapter.HistoryRecordsAdapter;
import com.cos.hospital.ui.record.RecordEditActivity;
import com.google.gson.JsonObject;
import com.jude.utils.JUtils;
import com.liaoinstan.springview.container.DefaultHeader;
import com.liaoinstan.springview.widget.SpringView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;

/**
 * 历史病历
 */
public class HistoryRecordsActivity extends BaseActivity {
    @BindView(R.id.springView)
    SpringView springView;
    @BindView(R.id.rc)
    RecyclerView rc;
    private BaseQuickAdapter adapter;
    private List<HistoryRecordsItemsBean> list;
    private int patientId;
    private int pageIndex = 1;

    @Override
    protected int layout() {
        return R.layout.activity_list;
    }

    @Override
    protected void init() {
        patientId = getIntent().getIntExtra(Code.SP.ID, 0);
        setTitleLeftIcon();
        setTitleCenterTxt("历史病历");

        springView.setHeader(new DefaultHeader(context));
        list = new ArrayList<>();
        adapter = new HistoryRecordsAdapter(list);
        adapter.openLoadAnimation();
        rc.setPadding(0, JUtils.dip2px(9), 0, 0);
        rc.setLayoutManager(new LinearLayoutManager(this));
        adapter.setEmptyView(R.layout.lay_empty, rc);
        rc.setAdapter(adapter);
    }

    @Override
    protected void logic() {
        springView.setListener(new MyOnFreshListener() {
            @Override
            public void onRefresh() {
                pageIndex = 1;
                getList();
            }
        });
        adapter.setOnLoadMoreListener(() -> {
            pageIndex++;
            getList();
        }, rc);
        adapter.setOnItemClickListener((adapter, view, position) -> toDetails(position));
        springView.callFreshDelay();
    }

    private void toDetails(int position) {
        HistoryRecordsItemsBean itemsBean = list.get(position);
        int fldMedicalHistoryId = itemsBean.getFldMedicalHistoryId();
        Intent intent = new Intent(this, RecordEditActivity.class);
        intent.putExtra(Code.SP.ID, fldMedicalHistoryId);
        intent.putExtra(Code.SP.EDITABLE, false);
        startActivity(intent);
    }

    private void getList() {
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("fldPatientId", patientId);
        jsonObject.addProperty("pageIndex", pageIndex);
        jsonObject.addProperty("pageSize", 10);
        UHttp.start(getServes().queryMedicalRecordList(jsonObject), "历史病历查询", new HttpMsgListener<ResultBean<HistoryRecordsBean>>() {
            @Override
            public void onSubscribe(Disposable disposable) {
                addEvent(disposable);
            }

            @Override
            public void onError(String describe) {
                USnackbar.showWarning(context, describe);
            }

            @Override
            public void onFinish() {
                springView.onFinishFreshAndLoadDelay();
            }

            @Override
            public void onSuccess(ResultBean<HistoryRecordsBean> dto) {
                if (dto.isSuccess()) {
                    HistoryRecordsBean resultBean = dto.getResult();
                    if (resultBean != null) {
                        int pageIndex = resultBean.getPageIndex();
                        int pageSize = resultBean.getPageSize();
                        int recordCount = resultBean.getRecordCount();
                        if (pageIndex == 1) {
                            list.clear();
                            adapter.notifyDataSetChanged();
                        }
                        List<HistoryRecordsItemsBean> itemsBeanList = resultBean.getItems();
                        if (itemsBeanList != null && itemsBeanList.size() != 0) {
                            list.addAll(itemsBeanList);
                        }
                        int totalPageCount = MyUtil.getInstance().getTotalPageCount(pageSize, recordCount);
                        if (totalPageCount <= pageIndex) {
                            adapter.loadMoreEnd();
                        } else {
                            adapter.loadMoreComplete();
                        }
                    }
                } else {
                    ErrorBean errorBean = dto.getError();
                    if (errorBean != null) {
                        showNotice(UTxt.isNull(errorBean.getMessage(), Code.DefaultPrompt));
                    } else {
                        showNotice(Code.DefaultPrompt);
                    }
                }
            }
        });
    }

    @OnClick({R.id.title_back})
    public void click(View view) {
        switch (view.getId()) {
            case R.id.title_back:
                finish();
                break;
        }
    }
}
